
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class EquipModify extends JFrame{
    public EquipModify(String userid, Object idDevice){

        SQLOperation sql=new SQLOperation();
        setTitle("Equipment Modify");
        Container container=getContentPane();
        setLayout(new BorderLayout());

        JPanel titlePanel=new JPanel();

        JLabel title=new JLabel("Modify Information of "+idDevice);
        title.setFont(new Font("",Font.BOLD,20));

        titlePanel.add(title);

        add(titlePanel,BorderLayout.NORTH);




        JPanel select=new JPanel();
        JComboBox enter=new JComboBox();

        JComboBox attributeSelect=new JComboBox();
        attributeSelect.addItem("----------");
        attributeSelect.addItem("DeviceType_typeID");
        attributeSelect.addItem("Manufacture_mUID");
        attributeSelect.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange()==ItemEvent.DESELECTED){
                    enter.removeAllItems();
                }
                else if(e.getStateChange()==ItemEvent.SELECTED&&e.getItem().toString()=="DeviceType_typeID") {
                    String[] deviceTypeAttri={"typeID"};
                    Object[][] deviceTypeInfo=sql.search("DeviceType", deviceTypeAttri,
                            "shanchu",1);
                    for(int i=0;i<deviceTypeInfo.length;i++) enter.addItem(deviceTypeInfo[i][0]);

                }
                else if(e.getStateChange()==ItemEvent.SELECTED&&e.getItem().toString()=="Manufacture_mUID"){
                    String[] factoryAttri={"mUID"};
                    Object[][] factoryInfo=sql.search("Manufacture",factoryAttri,
                            "shanchu",1);
                    for(int i=0;i<factoryInfo.length;i++) enter.addItem(factoryInfo[i][0]);
                }
            }
        });


        select.add(attributeSelect);
        select.add(enter);

        add(select,BorderLayout.CENTER);

        JButton back=new JButton("Back");
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new FamilySearchResult(userid,idDevice);
            }
        });

       JButton modify=new JButton("Modify it");
        modify.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(attributeSelect.getSelectedIndex()==0){
                    JOptionPane.showMessageDialog
                            (null, "Fail Modify Update!","Wrong Input",JOptionPane.ERROR_MESSAGE);

                }
                else{
                    sql.modify("Device","idDevice",idDevice,
                            (String)attributeSelect.getSelectedItem(),enter.getSelectedItem());
                    JOptionPane.showMessageDialog
                            (null, "Modify Complete!","Success",JOptionPane.INFORMATION_MESSAGE);
                    setVisible(false);
                    new EquipSearchResult(userid,idDevice);

                }


            }
        });



        JPanel buttons=new JPanel();
        buttons.add(back);
        buttons.add(modify);

        add(buttons,BorderLayout.SOUTH);



        setSize(400,150);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setVisible(true);

    }
}
